﻿using BLL;
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.SelfUtils;

namespace Web
{
    public partial class SearchGoods : System.Web.UI.Page
    {
        Pager pager;
        protected void Page_Load(object sender, EventArgs e)
        {
            pager = ViewState["pager"] as Pager;
            if (!IsPostBack)
            {
                string GoodsName="";
                if (Request.QueryString["Search"] != null)
                {
                    GoodsName = Request.QueryString["Search"].ToString();
                }
                Pager pager1 = new Pager();
                ViewState["pager"] = pager1;//页面重新加载后更新分页对象
                List<GoodsModels> list = GoodsBLL.selectByname(GoodsName, 10,1 );
                if (list.Count == 0)
                {
                    text_tips.Text = "你搜索的结果为空！请检查关键字";
                }
                else
                {
                    addrepeater(GoodsName, pager1);
                }
            }

        }
        void addrepeater(string GoodsName, Pager pager)
        {
            pager.Count = GoodsBLL.selectBysearch(GoodsName).Count;//总记录数
            //当前页码/总页码
            Label1.Text = string.Format("{0}/{1}", pager.PageIndex, pager.PageCount);
            Repeater1.DataSource = GoodsBLL.selectByname(GoodsName,pager.PageSize,pager.PageIndex);
            Repeater1.DataBind();
        }
        //首页
        protected void Button3_Click(object sender, EventArgs e)
        {
            string GoodsName = "";
            if (Request.QueryString["Search"] != null)
            {
                GoodsName = Request.QueryString["Search"].ToString();
            }
            pager.FirstPage();
            addrepeater(GoodsName, pager);
        }
        //上一页
        protected void Button4_Click(object sender, EventArgs e)
        {
            string GoodsName = "";
            if (Request.QueryString["Search"] != null)
            {
                GoodsName = Request.QueryString["Search"].ToString();
            }
            pager.PrevPage();
            addrepeater(GoodsName, pager);
        }
        //指定页
        protected void Button7_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text.Trim() != "")
            {
                string GoodsName = "";
                if (Request.QueryString["Search"] != null)
                {
                    GoodsName = Request.QueryString["Search"].ToString();
                }
                int i = int.Parse(TextBox1.Text.Trim());
                pager.ToPage(i);
                addrepeater(GoodsName, pager);
            }

        }
        //下一页
        protected void Button5_Click(object sender, EventArgs e)
        {
            string GoodsName = "";
            if (Request.QueryString["Search"] != null)
            {
                GoodsName = Request.QueryString["Search"].ToString();
            }
            pager.NextPage();
            addrepeater(GoodsName, pager);
        }
        //尾页
        protected void Button6_Click(object sender, EventArgs e)
        {
            string GoodsName = "";
            if (Request.QueryString["Search"] != null)
            {
                GoodsName = Request.QueryString["Search"].ToString();
            }
            Pager pager = ViewState["pager"] as Pager;
            pager.LastPage();
            addrepeater(GoodsName, pager);
        }
    }
}